#!/usr/bin/env python
# -*- coding: UTF-8 -*-

try:
	import pygame
	import sys
	from pygame.locals import *
	import langutils
except ImportError, err:
	print "Could not load %s module." % (err)
	sys.exit(2)

class InfoBar(pygame.sprite.DirtySprite):
	"""
	Object for the Info Bar at the bottom
	"""
	def __init__(self, field):
		pygame.sprite.DirtySprite.__init__(self)

		#loads, scales and transparencies the info bar image
		self.base_image = pygame.transform.scale(langutils.load_image('./data/rescs/DOWNBAR.pcx'), (800, 100))
		self.base_image.set_colorkey((255, 67, 255))
		#init some variables
		self.rect = self.base_image.get_rect()
		self.rect.topleft = (0, 500)
		self.data = {}
		self.terrain_imgs = []
		self.image = pygame.Surface((800, 100))
		self.image.set_colorkey((255, 67, 255))
		self.x = 0
		self.y = 0
		self.field_moved = False
		self.battlefield = field

		#needs something fixed width for consistency
		self.font = langutils.get_window_font()

		#placeholder for later options on terrain mini-images
		self.load_terrain_images('./data/maps/terrain.pcx')

	def load_terrain_images(self, file):
		"""
		Loads the terrain mini-images into the object
		"""
		img = langutils.load_image(file)
		w, h = img.get_size()
		w = w / 48
		h = h / 32

		#Subsurface + scales all of the mini-images at 48x32 to 60x40 size
		for y in xrange(h):
			for x in xrange(w):
				self.terrain_imgs.append(pygame.transform.scale(img.subsurface((x * 48, y * 32, 48, 32)), (60, 40)))




	def update(self):
		"""
		Updates the Info Bar
		"""

		x = self.cursor.x / 48 + (-self.battlefield.x + self.battlefield.right_overage) / 48
		y = self.cursor.y / 48 + (-self.battlefield.y + self.battlefield.bottom_overage) / 48

		#Hack so it doesn't update until finish scrolling field
		field_moved = False
		if self.battlefield.animating == [[],[]]:
			if self.field_moved == True:
				field_moved = True
			else:
				self.field_moved = True
		else:
			self.field_moved = False


		if (self.x != x or self.y != y) and self.cursor.animating == [[],[]] and field_moved:
			self.field_moved = False
			#gets data from relevant tile to be displayed
			self.data['terrain'], self.data['bonus'] = self.battlefield[y][x].get_data()[2:4]

			#sets new x and y values
			self.x = x
			self.y = y

			bonus = "%+3d%%" % (self.data['bonus'],)

			self.image.fill((0,0,0))
			self.image.blit(self.base_image, (0, 0))
			self.image.blit(self.font.render(bonus , True, (255, 255, 255)), (80, 42))
			self.image.blit(self.font.render(bonus , True, (255, 255, 255)), (80, 62))
			self.image.blit(self.terrain_imgs[self.data['terrain']], (20, 40))


	def set_cursor(self, cursor):
		"""
		Sets the cursor so its position can be found
		"""
		self.cursor = cursor

	def move(self, key, mod):
		"""
		Passes movement calls from the engine as the Info Bar doesn't move
		"""
		pass

	def r_move(self, key, mod):
		"""
		Passes movement calls from the engine as the Info Bar doesn't move
		"""
		pass
